package cae.logic;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

import org.apache.log4j.Logger;


public class CaeMessageReader
{
	private LineNumberReader fileReader;
	private static Logger logger = Logger.getLogger(CaeMessageReader.class);
	
	public CaeMessageReader(String file) throws FileNotFoundException
	{
		fileReader = new LineNumberReader(new FileReader(file));
	}
	
	public boolean ready()
	{
		try {
			return fileReader.ready();
		} catch (IOException e) {
			return false;
		}
	}
	
	public CaeMessage getMessage()
	{
		String[] msg = null;
		try {
			msg = fileReader.readLine().split("\\|\\|");
		} catch (IOException e) {
			logger.error("No se puede leer la linea ", e);
		}
		if(msg.length != 2)
		{
			logger.error("No se puede leer la linea, formato incorrecto. ");
			return null;
		}
		CaeMessage cMsg = new CaeMessage(msg[0], msg[1]);
		return cMsg;
	}
}
